﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using HTML5Converter.Entities;

namespace HTML5Converter
{
    /// <summary>
    /// Interaction logic for UC_AddContent.xaml
    /// </summary>
    public partial class UC_AddContent : UserControl
    {
        public UC_AddContent()
        {
            InitializeComponent();
            BindGrid();
            grdAddContent.ItemsSource = LstAddContent;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtContent.Text))
            {
                MessageBox.Show("Content cannot be empty");
                return;
            }
            if (cmbWhere.SelectedIndex <= 0)
            {
                MessageBox.Show("Please select the tag where the contnet should be added.");
                return;
            }
            if (cmbContentType.SelectedIndex <= 0)
            {
                MessageBox.Show("Please select the type of content");
                return;
            }
            if (string.Compare((cmbContentType.SelectedItem as ComboBoxItem).Name, "J", true) == 0)
            {
                if (!txtContent.Text.StartsWith("<script", StringComparison.InvariantCultureIgnoreCase))
                {
                    MessageBox.Show("Script Tag is missing");
                    return;
                }
                if ((!txtContent.Text.EndsWith("</script>", StringComparison.InvariantCultureIgnoreCase)) && !txtContent.Text.EndsWith("/>"))
                {
                    MessageBox.Show("End Script Tag is missing");
                    return;
                }
            }
            if (string.Compare((cmbContentType.SelectedItem as ComboBoxItem).Name, "S", true) == 0)
            {
                if (!txtContent.Text.StartsWith("<style", StringComparison.InvariantCultureIgnoreCase))
                {
                    MessageBox.Show("Style tag is missing.");
                    return;
                }
                if (!txtContent.Text.EndsWith("</style>", StringComparison.InvariantCultureIgnoreCase))
                {
                    MessageBox.Show("End style Tag is missing");
                    return;
                }
            }
            if (!new FileInfo(App.Html5RulesFilePath).Exists)
            {
                XmlTextWriter tw = new XmlTextWriter(App.Html5RulesFilePath, null);
                tw.WriteStartDocument();
                tw.WriteComment("This is Rules for HTML5 convertion");
                tw.WriteStartElement("Rules");

                tw.WriteStartElement("AddContent");

                tw.WriteStartElement("Content");
                tw.WriteString(txtContent.Text);
                tw.WriteEndElement();

                tw.WriteStartElement("Where");
                tw.WriteString((cmbWhere.SelectedItem as ComboBoxItem).Name);
                tw.WriteEndElement();

                tw.WriteStartElement("ContentType");
                tw.WriteString((cmbContentType.SelectedItem as ComboBoxItem).Name);
                tw.WriteEndElement();

                tw.WriteEndElement();
                tw.WriteEndElement();
                tw.WriteEndDocument();
                tw.Flush();
                tw.Close();
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(App.Html5RulesFilePath);
                XmlNode xmlRoot = doc.DocumentElement;
                XmlNode newNod = doc.CreateElement("AddContnet");
                xmlRoot.AppendChild(newNod);
                xmlRoot = newNod;

                newNod = doc.CreateElement("Content");
                XmlText nodText = doc.CreateTextNode(txtContent.Text);
                newNod.AppendChild(nodText);
                xmlRoot.AppendChild(newNod);

                newNod = doc.CreateElement("Where");
                nodText = doc.CreateTextNode((cmbWhere.SelectedItem as ComboBoxItem).Name);
                newNod.AppendChild(nodText);
                xmlRoot.AppendChild(newNod);

                newNod = doc.CreateElement("ContentType");
                nodText = doc.CreateTextNode((cmbContentType.SelectedItem as ComboBoxItem).Name);
                newNod.AppendChild(nodText);
                xmlRoot.AppendChild(newNod);

                doc.Save(App.Html5RulesFilePath);
            }
            LstAddContent.Add(new AddContent()
            {
                Content = txtContent.Text,
                ContentType = (ContentTypeEnum)(cmbContentType.SelectedItem as ComboBoxItem).Name[0],
                Where = (WhereEnum)(cmbWhere.SelectedItem as ComboBoxItem).Name[0]
            });
            BindGrid();
            (Application.Current.MainWindow as MainWindow).LoadXmlData();
            ResetFeilds();
        }

        private void BindGrid()
        {
            grdAddContent.ItemsSource = string.Empty;
            grdAddContent.ItemsSource = LstAddContent;
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            ResetFeilds();
        }
        private void ResetFeilds()
        {
            txtContent.Text = string.Empty;
            cmbContentType.SelectedIndex = 0;
            cmbWhere.SelectedIndex = 0;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            AddContent objAddContent = ((FrameworkElement)sender).DataContext as AddContent;
            LstAddContent.Remove(objAddContent);
            XDocument xdoc = XDocument.Load(App.Html5RulesFilePath); xdoc.Root.Descendants("AddContnet").Where(no => no.Element("Content").Value == objAddContent.Content && (WhereEnum)no.Element("Where").Value[0] == objAddContent.Where && (ContentTypeEnum)no.Element("ContentType").Value[0] == objAddContent.ContentType).Remove();
            xdoc.Save(App.Html5RulesFilePath);
            BindGrid();
        }

        public List<AddContent> LstAddContent
        {
            get
            {
                return (Application.Current.MainWindow as MainWindow).ListAddContent;
            }
        }

        private void cmbWhere_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void cmbContentType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ContentTypeEnum objContentType= (ContentTypeEnum)(cmbContentType.SelectedItem as ComboBoxItem).Name[0];
            switch (objContentType)
            {
                case ContentTypeEnum.style:
                    cmbWhere.SelectedIndex = 1;
                    cmbWhere.IsEnabled = false;
                    break;
                case ContentTypeEnum.JavaScript:
                    cmbWhere.SelectedIndex = 2;
                    cmbWhere.IsEnabled = false;
                    break;
                case ContentTypeEnum.HTMLTag:
                    cmbWhere.SelectedIndex = 2;
                    cmbWhere.IsEnabled = false;
                    break;
                case ContentTypeEnum.Other:                    
                    cmbWhere.IsEnabled = true;
                    break;
                default:
                    break;
            }
        }

        private void grdAddContent_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
